<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends MY_Model
{	
	function User_model()
	{
		parent::MY_Model();
	}
	
	function get_all($type,$level, $start, $numItem, $keyword='')
	{	
		$data = array();
		$data['lstUser'] = array();
		$result = $this->getRowsOnMultiQuery("CALL nm_admin_user_getall('$type',$level, '$start', '$numItem','$keyword');");
        $data['lstUser'] = $result[0];
		$data['iCount'] = 0;
		$data['iCount'] = (int)$result[1][0]['num'];
		$data['pCount'] = ($data['iCount'] > $numItem) ? ceil($data['iCount']/$numItem) : 1;
		return $data;
	}
	
	function add($username, $password, $contactName, $email, $phone, $address, $roleid, $block, $active)
	{
		$uidAction = (int)$this->session->userdata('uid');
		$this->exeQuery("CALL nm_user_insert('$username','$password','$contactName','$email','$phone','$address',$roleid,$block,$active,$uidAction)");
	}
	
	function update($uid, $password, $contactName, $email, $phone, $address, $block, $active, $roleid)
	{
		$ci = & get_instance();
		$uidAction = (int)$ci->session->userdata('uid');
		$this->exeQuery("CALL nm_admin_user_update('$uid','$password','$contactName','$email','$phone','$address','$block','$active','$roleid','$uidAction');");
	}
	
	function check_username_available($uname)
	{
		$row = $this->getRows("CALL nm_user_check_username_available('$uname');", 0);
		$chk = false;
		if($row[0] == 0)
		{
			$chk = true;
		}
		return $chk;
	}
	
	function check_email_available($email)
	{
		$row = $this->getRows("CALL nm_user_check_username_available('$uname');", 0);
		$chk = false;
		if($row[0] == 0)
		{
			$chk = true;
		}
		return $chk;
	}
	
	function get_user_info($userid)
	{
		return $this->getRows("CALL nm_get_user_info('$userid');", 0);
	}
	
	function checkEmail($email) {
		$this->load->helper('email');
		if(valid_email($email))
		{
			return true;
		}
		return false;
	}
	    
}